package com.xinmao.quantitative.chart;

import com.xinmao.quantitative.utils.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.TimeSeriesCollection;
import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.TradingRecord;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.num.Num;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @Title: CashFlowToChart
 * @Author: XYe13
 * @Date: 5/20/2024 10:55
 * @Description:
 */
public class DataFlowToChart {
    public static void getChartDay(BarSeries series, Indicator<Num> indicator, TradingRecord tradingRecord,String mainName1, String mainName2) {
        System.setProperty("java.awt.headless", "false");
        /*
         * Building chart datasets
         */
        TimeSeriesCollection datasetAxis1 = new TimeSeriesCollection();
        datasetAxis1.addSeries(ChartUtil.buildChartBarSeriesDay(series, new ClosePriceIndicator(series), mainName1));
        TimeSeriesCollection datasetAxis2 = new TimeSeriesCollection();
        datasetAxis2.addSeries(ChartUtil.buildChartBarSeriesDay(series, indicator, mainName2));
        /*
         * Creating the chart
         */
        creatingChart(series, tradingRecord,datasetAxis1,datasetAxis2);
    }

    public static void getChartMin(BarSeries series, Indicator<Num> indicator, TradingRecord tradingRecord, String mainName1, String mainName2) {
        System.setProperty("java.awt.headless", "false");
        /*
         * Building chart datasets
         */
        TimeSeriesCollection datasetAxis1 = new TimeSeriesCollection();
        datasetAxis1.addSeries(ChartUtil.buildChartBarSeriesMinute(series, new ClosePriceIndicator(series), mainName1));
        TimeSeriesCollection datasetAxis2 = new TimeSeriesCollection();
        datasetAxis2.addSeries(ChartUtil.buildChartBarSeriesMinute(series, indicator, mainName2));
        /*
         * Creating the chart
         */
        creatingChart(series, tradingRecord,datasetAxis1,datasetAxis2);
    }

    public static void creatingChart(BarSeries series, TradingRecord tradingRecord,TimeSeriesCollection datasetAxis1,
                                     TimeSeriesCollection datasetAxis2) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(series.getName(), // title
                "Date", // x-axis label
                "Price", // y-axis label
                datasetAxis1, // data
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));

        /*
         * Adding the  flow axis (on the right)
         */
        ChartUtil.addDataFlowAxis(plot, datasetAxis2);
        if (tradingRecord != null) {
            ChartUtil.addBuySellSignals(series, tradingRecord, plot);
        }

        try {
            ChartUtils.saveChartAsPNG(new File("chart.png"), chart, 1024, 400);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
